﻿using Dapper.DBContext.Schema;
using Guoc.BigMall.Infrastructure;
using Guoc.BigMall.Infrastructure.Extension;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Guoc.BigMall.Domain.Entity
{
      /// <summary>
    ///  分销订单
    /// </summary>
    public class DistributeOrder : BaseEntity
    {
        public DistributeOrder()
        {
            this.CreatedOn = DateTime.Now;
            this.Status = DistributeOrderStatus.Create;
            this.Items = new List<DistributeOrderItem>();
            this.UpdatedOn = this.CreatedOn;
            this.Historys = new List<DistributeOrderHistory>();
        }

        public string Code { get; set; }

        public string SaleOrderCode { get; set; }

        public int SaleOrderId { get; set; }

        /// <summary>
        /// 下单会员
        /// </summary>
        public int MemberId { get; set; }
      
        /// <summary>
        ///  真实姓名
        /// </summary>

        public string MemberName { get; set; }

        /// <summary>
        ///  开卡电话（开卡电话将和微信绑定）
        /// </summary>

        public string MemberPhone { get; set; }
        /// <summary>
        /// 微信昵称
        /// </summary>
        public string NickName { get; set; }
        /// <summary>
        /// 办卡人联系电话
        /// </summary>
        public string ContactPhone { get; set; }
        /// <summary>
        ///  办卡 身份证
        /// </summary>
        public string IdentityCard { get; set; }

        public int ReferMemberId { get; set; }

        public DateTime CreatedOn { get; set; }

        public DateTime UpdatedOn { get; set; }

        public DistributeOrderStatus Status { get; set; }

        public int StoreId { get; set; }

        public string StoreCode { get; set; }

        public List<DistributeOrderItem> Items { get; set; }

        public List<DistributeOrderHistory> Historys { get; set; }

        /// <summary>
        /// 支付后，待审
        /// </summary>
        public void WaitAudit()
        {
            if (this.Status != DistributeOrderStatus.Create)
            {
                throw new FriendlyException("必须是新订单");
            }
            this.Status = DistributeOrderStatus.WaitToAudit;
            this.UpdatedOn = DateTime.Now;
        }

        public void AuditPhone()
        {           
            if (this.Status != DistributeOrderStatus.WaitToAudit) {
                throw new FriendlyException("必须是待审订单");
            }
            this.Status = DistributeOrderStatus.Audited;
            this.UpdatedOn = DateTime.Now;            
        }

        public void ConfirmCash()
        {
            if (this.Status != DistributeOrderStatus.Audited)
            {
                throw new FriendlyException("必须已审订单");
            }
            this.Status = DistributeOrderStatus.CanPay;
            this.UpdatedOn = DateTime.Now;
        }

        /// <summary>
        ///  已提现,完结订单
        /// </summary>
        public void Finished()
        {
            if (this.Status == DistributeOrderStatus.CanPay || this.Status == DistributeOrderStatus.Finish)
            {
                this.Status = DistributeOrderStatus.Finish;
                this.UpdatedOn = DateTime.Now;
            }
            else {
                throw new FriendlyException("必须是可提现状态");
            }
          
        }
    }


    public enum DistributeOrderStatus
    {
        [Description("已取消")]
        Cancel = -1,
        [Description("待支付")]
        Create = 1,
        [Description("待审")]
        WaitToAudit = 2,
        [Description("已审")]
        Audited = 3,
        /// <summary>
        /// 可提现
        /// </summary>
        [Description("可提现")]
        CanPay = 4,
        /// <summary>
        /// 已完成
        /// </summary>
        [Description("已完成")]
        Finish = 5

    }
}
